using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnInit(EventArgs e)
        {
            //String html = " <input id=\"insert\" name=\"insert\" value=\"Click me!\" style=\"Z-INDEX:{101 + position()};POSITION: absolute;TOP: {position() * 40}px;LEFT: 10px\" type=\"button\" OnServerClick=\"Button_Click\" runat=\"server\"/>";
            //Control ctrl = Page.ParseControl(html);


            //HtmlInputButton button = ctrl.Controls[0] as HtmlInputButton;
            //if (null != button)
            //{
            //    button.ServerClick += new System.EventHandler(this.Button_Click);
            //    form1.Controls.Add(button);
            //}

            //String html1 = "<input type=\"text\" id=\"name\" name=\"name\" value=\"le duc son\" runat=\"server\" />";
            //Control ctrl1 = Page.ParseControl(html1);
            //HtmlInputText input = ctrl1.Controls[0] as HtmlInputText;
            //if (null != input)
            //{
            //    form1.Controls.Add(input);
            //}
            //UserControl uc = (UserControl)Page.LoadControl("KYCDropDownList.ascx");
            //((KYCDropDownList)uc).CssClass = "InputBox";
            //((KYCDropDownList)uc).ControlID = "cityDropDownList";
            //((KYCDropDownList)uc).Name = "City";
            //List<ListItem> listItemArray = new List<ListItem>();
            //listItemArray.Add(new ListItem("TpHCM","1"));
            //listItemArray.Add(new ListItem("HaNoi","2"));
            //listItemArray.Add(new ListItem("DaNang", "3"));
            //((KYCDropDownList)uc).ListItemArray = listItemArray;
            //((KYCDropDownList)uc).SeletedValue = "3";
            //form1.Controls.Add(uc);
            //UserControl uc = (UserControl)Page.LoadControl("KYCTextBox.ascx");
            //((KYCTextBox)uc).CssClass = "InputBox";
            //((KYCTextBox)uc).ControlID = "emailTextBox";
            //((KYCTextBox)uc).Name = "Email";
            ////form1.Controls.Add(uc);
            //UserControl uc1 = (UserControl)Page.LoadControl("KYCTextBox.ascx");
            //((KYCTextBox)uc1).CssClass = "InputBox";
            //((KYCTextBox)uc1).ControlID = "addressTextBox";
            //((KYCTextBox)uc1).Name = "Address";
            //((KYCTextBox)uc1).IsRequire = true;
            //((KYCTextBox)uc1).RequireMessage = "Field Is Require";
            //form1.Controls.Add(uc1);
            Object[] objParams = new object[] { 12 };
            UserControl uc1 = (UserControl)Page.LoadControl(typeof(KYCField), objParams);
            //((KYCField)uc1).KYCSetAttributeID = 12;
            form1.Controls.Add(uc1);

            objParams = new object[] { 11 };
            uc1 = (UserControl)Page.LoadControl(typeof(KYCField), objParams);
            //((KYCField)uc1).KYCSetAttributeID = 12;
            form1.Controls.Add(uc1);

            base.OnInit(e);
        }

        

        protected void Button_Click(object sender, EventArgs e)
        {


        }



        protected void CreateColumn()
        {

        }


        /// <summary>
        /// create and set value for Textbox control
        /// </summary>
        /// <param name="controlID">Control ID </param>
        /// <param name="value">Value of control</param>
        /// <returns></returns>
        protected TextBox TextBoxControl(string controlID, string value)
        {
            TextBox textbox = new TextBox();
            textbox.ID = controlID;
            SetControlValue(textbox, value);
            return textbox;
        }


        protected DropDownList DropDownListControl(string controlID, List<ListItem> listItemArray, string value)
        {
            DropDownList dropDownList = new DropDownList();
            dropDownList.ID = controlID;

            for (int i = 0; i < listItemArray.Count; i++)
            {
                ListItem listItem = listItemArray[i];
                dropDownList.Items.Add(listItem);
            }
            SetControlValue(dropDownList, value);
            return dropDownList;
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {


        }






        public void SetControlValue(Control control, string value)
        {
            Type _ctlType = control.GetType();
            switch (_ctlType.Name)
            {
                case "TextBox":
                    SetTextBox(control, value);
                    break;
                case "DropDownList":
                    SetDropDownList(control, value);
                    break;
                case "ListBox":
                    SetListBox(control, value);
                    break;
                case "CheckBox":
                    SetCheckBox(control, value);
                    break;
                case "CheckBoxList":
                    SetCheckBoxList(control, value);
                    break;
                case "RadioButton":
                    SetRadioButton(control, value);
                    break;
                case "RadioButtonList":
                    SetRadioButtonList(control, value);
                    break;
                default:
                    break;
            }
        }

        private void SetTextBox(Control control, string value)
        {
            TextBox _ctl = (TextBox)control;
            _ctl.Text = value;
        }

        private void SetDropDownList(Control control, string value)
        {
            DropDownList _ctl = (DropDownList)control;
            _ctl.SelectedIndex = _ctl.Items.IndexOf(_ctl.Items.FindByValue(value));
        }

        private void SetListBox(Control control, string value)
        {
            ListBox _ctl = (ListBox)control;
            ArrayList values = StringToArrayList(value);

            foreach (ListItem li in _ctl.Items)
            {
                if (values.Contains(li.Value))
                    li.Selected = true;
                else
                    li.Selected = false;
            }
        }

        private void SetCheckBox(Control control, string value)
        {
            CheckBox _ctl = (CheckBox)control;
            _ctl.Checked = Convert.ToBoolean(value);
        }

        private void SetCheckBoxList(Control control, string value)
        {
            CheckBoxList _ctl = (CheckBoxList)control;
            ArrayList values = StringToArrayList(value);

            foreach (ListItem li in _ctl.Items)
            {
                if (values.Contains(li.Value))
                    li.Selected = true;
                else
                    li.Selected = false;
            }
        }

        private void SetRadioButton(Control control, string value)
        {
            RadioButton _ctl = (RadioButton)control;
            _ctl.Checked = Convert.ToBoolean(value);
        }

        private void SetRadioButtonList(Control control, string value)
        {
            RadioButtonList _ctl = (RadioButtonList)control;
            ArrayList values = StringToArrayList(value);

            // There is no enumerator for the RadioButtonList class so
            // the RadioButtonList needs to iterate by index
            for (int i = 0; i < _ctl.Items.Count; i++)
            {
                ListItem li = _ctl.Items[i];

                if (values.Contains(li.Value))
                    li.Selected = true;
                else
                    li.Selected = false;
            }
        }

        private ArrayList StringToArrayList(string value)
        {
            ArrayList _al = new ArrayList();
            string[] _s = value.Split(new char[] { ',' });

            foreach (string item in _s)
                _al.Add(item);

            return _al;
        }

        private string ArrayListToString(ArrayList value)
        {
            StringBuilder _s = new StringBuilder();
            int _count = 0;

            foreach (string item in value)
            {
                _s.Append(item);

                if (_count < value.Count - 1)
                    _s.Append(",");

                _count++;
            }

            return _s.ToString();
        }

        protected void textbox_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}

